<template>
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
            >
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['zwsj:net-sys-config:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="编号" align="center" prop="id" width="55" />
          <el-table-column label="社区/中间件地址" align="left" prop="community.name" min-width="160">
            <template #default="scope">
              <div>
                {{scope.row.community.name}}
                <span style="color:red" v-if="scope.row.levelNames">支持会员：{{ scope.row.levelNames }}</span>
              </div>
              <div>{{scope.row.lfApiUrl }}</div>
            </template>
          </el-table-column>
          <el-table-column label="在线状态/时间" align="left" width="170">
            <template #default="scope">
              <div v-if="scope.row.onlineStatus == 1">在线</div>
              <div v-else style="color: red">不在线</div>
              {{formatDate(scope.row.lastOnlineTime) }}
            </template>
          </el-table-column>
          <el-table-column label="开关" align="left" width="150">
            <template #default="scope">
              <template v-if="true == scope.row.pullAccountBeforeBindFlag">
                  <div style="color:red;">绑定前拉取账号</div>
              </template>
              <template v-else>
                <div>不自动拉取账号</div>
              </template>

              
              <template v-if="true == scope.row.supportSelfBindFlag">
                  <div style="color:red;">支持自行绑定/解绑</div>
              </template>
              <template v-else>
                <div>不支持自行绑定/解绑</div>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="分账模式" align="left" width="160">
            <template #default="scope">
              <div>模式：{{ scope.row.houseDivideFlag == true ? "按楼栋分账" : "不按楼栋分账" }}</div>
              <div>规则：<dict-tag :type="DICT_TYPE.NET_DIVIDE_MODE" :value="scope.row.divideMode" /></div>
            </template>
          </el-table-column>
          <el-table-column label="兜底账户" align="left" prop="underTakeMatCode" width="200">
            <template #default="scope">
              <div>账户：{{scope.row.underTakeMatName_}}</div>
              <div>编码：{{ scope.row.underTakeMatCode }}</div>
            </template>
          </el-table-column>
          <el-table-column label="分账账户1/分账值1" align="left" prop="divideMatUserName1_" width="180">
            <template #default="scope">
              <div>账户1：{{scope.row.divideMatUserName1_}}</div>
              <div>值1：{{ scope.row.divideMatValue1 }}</div>
            </template>
          </el-table-column>
          <el-table-column label="分账账户2/分账值2" align="left" prop="divideMatUserName2_" width="180">
            <template #default="scope">
              <div>账户2：{{scope.row.divideMatUserName2_}}</div>
              <div>值2：{{ scope.row.divideMatValue2 }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['zwsj:net-sys-config:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['zwsj:net-sys-config:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <NetSysConfigForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter,formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import * as NetSysConfigApi from '@/api/zwsj/netSysConfig'
import NetSysConfigForm from './NetSysConfigForm.vue'
import CommunityTree from '@/views/zwsj/common/CommunityTree.vue'
import { ElNotification } from 'element-plus'

defineOptions({ name: 'NetSysConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  commId: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await NetSysConfigApi.getNetSysConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 处理社区被点击 */
const handleCommunityNodeClick = async (row) => {
  queryParams.commId = row.id
  await getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, commId?: number) => {
  formRef.value.open(type, id, commId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await NetSysConfigApi.deleteNetSysConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
